/**
 * Coryright 2015-2020 www.eyaomall.cn
 * All rights reserved.
 * @author huwenqing
 * @date 2016年9月20日
 *
 */

package com.eyaomall.wxmall.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmall.model.po.Advert;
import com.eyaomall.wxmall.model.vo.AdvertPlaceVO;
import com.eyaomall.wxmall.service.AdvertService;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;

/**
 * 广告controller
 * 
 * @author hwq
 */
@Controller
@RequestMapping("advert")
public class AdvertController {

	@Autowired
	private AdvertService advertService;

	@RequestMapping("page_data")
	public @ResponseBody GsnData pageData(Advert advert, PageHTTPObject pageHTTPObject,Map<String,Object> data) {
		List<Advert> advertList = advertService.listPage("3");
		
		//根据广告ID
		Map<Integer,List<Advert>> dataMap = new HashMap<Integer,List<Advert>>();
		for(Advert ad : advertList){
			if("3".equalsIgnoreCase(ad.getType())){
				List<Advert> aList = dataMap.get(ad.getPlaceId());
				if(aList == null){
					aList = new ArrayList<Advert>();
				}
				
				aList.add(ad);
				dataMap.put(ad.getPlaceId(), aList);
				
			}
			
		}
		
		List<AdvertPlaceVO> list = new ArrayList<AdvertPlaceVO>();
		Iterator<Integer> it = dataMap.keySet().iterator();
		while(it.hasNext()){
			List<Advert> pList = dataMap.get(it.next());
			AdvertPlaceVO pVO = new AdvertPlaceVO();
			pVO.setList(pList);
			
			if(pList != null && !pList.isEmpty()){
				int colNum = Integer.parseInt(pList.get(0).getShowWay()) ;
				int rowNum = pList.get(0).getShowNum();
				
				pVO.setColNum(colNum);
				pVO.setRowNum(rowNum);
				pVO.setTitle(pList.get(0).getTitle());
			}
			
			list.add(pVO);
		}
		
		return GsnResponse.toData(list);
	}

}
